/*
	Copyright (c) 2009 - 2010 conleon1988 (conleon1988@gmail.com)

	Permission is hereby granted, free of charge, to any person
	obtaining a copy of this software and associated documentation
	files (the "Software"), to deal in the Software without
	restriction, including without limitation the rights to use,
	copy, modify, merge, publish, distribute, sublicense, and/or sell
	copies of the Software, and to permit persons to whom the
	Software is furnished to do so, subject to the following
	conditions:

	The above copyright notice and this permission notice shall be
	included in all copies or substantial portions of the Software.

	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
	EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
	OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
	NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
	HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
	WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
	FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
	OTHER DEALINGS IN THE SOFTWARE.
*/

#include "stdafx.h"

#include <iostream>
#include <string>
#include <fstream>
#include <sstream>

template <class T>
std::string itoS(T t, std::ios_base & (*f)(std::ios_base&))
{
	std::ostringstream oss;
	oss << f << t;
	
	return oss.str();
}

static const unsigned int buildHash(const std::string& in);
static void writeStream(std::ofstream& stream,const std::string& buf,const bool resetBuf = false);
static std::string itoS(const unsigned int n);

int main(int argc, char* argv[])
{
	std::ofstream out("eeprom_hash.h",std::ios::out | std::ios::ate);

	writeStream(out,"#ifndef __eeprom_table__h",true);
	writeStream(out,"#define __eeprom_table__h",true);
	writeStream(out,"/*EEPROM HASHTABLE for SuperMD plugin*/",true);
	writeStream(out,"/*Generated by Conle's eeprom hash generator(source included in package)*/",true);
	writeStream(out,"/*(no std::hash map for C compatibility)*/",true);
	writeStream(out,"static const unsigned int EEPROM_HASHTABLE[] = {",true);

 	writeStream(out,"/*24C01*/",true);writeStream(out," ",true);
	writeStream(out,itoS<unsigned int>(buildHash("T-120106"),std::dec) + ",");
	writeStream(out,itoS<unsigned int>(buildHash("T-50176"),std::dec) + ",");
	writeStream(out,itoS<unsigned int>(buildHash("T-50396"),std::dec) + ",");
	writeStream(out,itoS<unsigned int>(buildHash("T-50446"),std::dec) + ",");
	writeStream(out,itoS<unsigned int>(buildHash("T-50516"),std::dec) + ",");
	writeStream(out,itoS<unsigned int>(buildHash("T-50606"),std::dec) + ",");
	writeStream(out,itoS<unsigned int>(buildHash("T-12046"),std::dec) + ",");
	writeStream(out,itoS<unsigned int>(buildHash("T-12053"),std::dec) + ",");
	writeStream(out,itoS<unsigned int>(buildHash("MK-1215"),std::dec) + ",");
	writeStream(out,itoS<unsigned int>(buildHash("MK-1228"),std::dec) + ",");
	writeStream(out,itoS<unsigned int>(buildHash("G-5538"),std::dec) + ",");
	writeStream(out,itoS<unsigned int>(buildHash("PR-1993"),std::dec) + ",");
	writeStream(out,itoS<unsigned int>(buildHash("G-4060"),std::dec) + ",");
	writeStream(out,itoS<unsigned int>(buildHash("G-4060-00"),std::dec) + ",");
	writeStream(out,itoS<unsigned int>(buildHash("00001211-00"),std::dec) + ",");
	writeStream(out,itoS<unsigned int>(buildHash("00004076-00"),std::dec) + ",");

 	writeStream(out,"/*24C02*/",true);writeStream(out," ",true);
	writeStream(out,itoS<unsigned int>(buildHash("T-081326"),std::dec) + ",");
	writeStream(out,itoS<unsigned int>(buildHash("T-81033"),std::dec) + ",");
	writeStream(out,itoS<unsigned int>(buildHash("T-81406"),std::dec) + ",");
	writeStream(out,itoS<unsigned int>(buildHash("T-081276"),std::dec) + ",");

	writeStream(out,"/*24C08 ( same as 24C16 00000000-00 game )*/",true);writeStream(out," ",true);
	writeStream(out,itoS<unsigned int>(buildHash("00000000-00"),std::dec) + ",");

 	writeStream(out,"/*24C16*/",true);writeStream(out," ",true);
	writeStream(out,itoS<unsigned int>(buildHash("T-081586"),std::dec) + ",");

 	writeStream(out,"/*24C65*/",true);writeStream(out," ",true);
	writeStream(out,itoS<unsigned int>(buildHash("T-81576"),std::dec) + ",");
	writeStream(out,itoS<unsigned int>(buildHash("T-81476"),std::dec) + ",");
	writeStream(out,itoS<unsigned int>(buildHash("T-120146-50"),std::dec) );
	writeStream(out,"};",true);
	writeStream(out," ",true);

	writeStream(out,"static const unsigned int EEPROM_HASHTABLE_LENGTH = sizeof(EEPROM_HASHTABLE) / sizeof(EEPROM_HASHTABLE[0]);",true);
	writeStream(out,"#endif //__eeprom_table__h",true);

	out.close();
	return 0;
}

static void writeStream(std::ofstream& stream,const std::string& buf,const bool resetBuf)
{
	static int outBuffer = 0;
	
	static const int digits = 10;
	static const int rows = 5;
	static const int limit = (rows * digits)-1;
	
	if((resetBuf) || (outBuffer > limit))
	{
		stream.write("\n\t\t",3);
		outBuffer = 0;
	}

	stream.write(buf.c_str(),buf.length());
	outBuffer += buf.length();
}

static const unsigned int buildHash(const std::string& in)
{
   unsigned int hash = 1315423911;

   for(int i = 0; i < in.length(); i++)
      hash ^= ((hash << 5) + in[i] + (hash >> 2));

   return hash;
}
